﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Phoenix.AppCode.Entities;
using System.Data.SqlClient;
using System.Data;

namespace Phoenix.AppCode.Data
{
    public class PortDAO : BaseDAO
    {
        public DataTable PortList(Port port)
        {
            using (SqlConnection connection = GetConnection())
            {
                connection.Open();
                SqlCommand command = new SqlCommand("sp_PortList", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter sda = new SqlDataAdapter(command);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                command.ExecuteNonQuery();
                return dt;
            }
        }

        #region Backend method
        /// <summary>
        /// Insert Article Category
        /// </summary>
        /// <param name="port"></param>
        public void Create(Port port)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spPortCreate", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@PortName", port.PortName));
                command.Parameters.Add(new SqlParameter("@PostUser", port.PostUser));

                connection.Open();
                command.ExecuteNonQuery();
                //if (paramId.Value != DBNull.Value)
                //Port.ID = (long)paramId.Value;
                //else
                //  throw new DataAccessException(String.Format(Resources.Strings.CreateEntityException, "Category"));
            }
        }

        /// <summary>
        /// Edit Article Category
        /// </summary>
        /// <param name="category"></param>
        public void Edit(Port port)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spPortEdit", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_Port", port.ID_Port));
                command.Parameters.Add(new SqlParameter("@PortName", port.PortName));
                command.Parameters.Add(new SqlParameter("@EditUser", port.EditUser));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.UpdateEntityException, "Category", category.ID));
            }
        }

        /// <summary>
        /// On/Off view
        /// </summary>
        /// <param name="port"></param>
        public void OnOff(Port port)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spPortOnOff", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_Port", port.ID_Port));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.UpdateEntityException, "Category", category.ID));
            }
        }


        /// <summary>
        /// Delete Port
        /// </summary>
        /// <param name="category"></param>
        public void Delete(Port port)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spPortDelete", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_Port", port.ID_Port));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.DeleteEntityException, "Category", category.ID));
            }
        }

        /// <summary>
        /// Show Port All
        /// </summary>
        /// <returns></returns>
        public DataTable PortList()
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spPortList", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;
            }
        }

        /// <summary>
        /// Show data by ID
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public DataTable PortList_GetByID(int idport)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spPortEdit_GetByID", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ID_Port", SqlDbType.Int).Value = idport;
                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;
            }
        }

        /// <summary>
        /// Get ID to Edit or Delete
        /// </summary>
        /// <param name="idport"></param>
        /// <returns></returns>
        public Port GetPortByID(int idport)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spPortEdit_GetByID", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_Port", idport));

                connection.Open();
                Port Port = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                        Port = CreatePortFromReader(reader);
                    //else
                    //   throw new DataAccessException(String.Format(Resources.Strings.GetEntityException, "Category", ID));
                }

                return Port;
            }
        }

        private Port CreatePortFromReader(IDataReader reader)
        {
            Port Port = new Port();
            Port.ID_Port = (int)reader["ID_Port"];
            Port.PortName = (string)reader["PortName"];
            Port.PostDate = (DateTime)reader["PostDate"];
            Port.PostUser = (string)reader["PostUser"];
            Port.EditDate = (DateTime)reader["EditDate"];
            Port.EditUser = (string)reader["EditUser"];
            Port.Visible = (Boolean)reader["Visible"];
            return Port;
        }
        #endregion
    }
}